#include "stdafx.h"
#include "MsResourceHelpers.h"
#include "IPALDebuggingServices.h"
#include "EncodedPtr.h"
#include "IPlatformServices.h"


extern EncodedPtr<IPlatformServices> gps;

HRESULT MsResourceHelpers::CrackMsResourceUri( IPALUri *pUri, CXString **ppResourceMap, CXString **ppResourcePath, CXString **ppFilePath )
{

	HRESULT hr = S_OK;
	unsigned int bufferLength = 0;
	XCHAR * pBuffer = NULL;
	void *pPathPrefixes[2];
	pPathPrefixes[0] = L"Files/";
	pPathPrefixes[1] = L"/Files/";
	CXString *pPathPrefix = NULL;
	CXString *pResourcePath = NULL;
	if ( ppFilePath )
		*ppFilePath = NULL;

	hr = pUri->GetHost(&bufferLength, NULL);
	if ( SUCCEEDED(hr) )
	{
		++bufferLength;
		pBuffer = (XCHAR * )new char[(2 * bufferLength | -((unsigned __int64)bufferLength >> 31 != 0))];
		if ( pBuffer )
		{
			hr = pUri->GetHost(&bufferLength, pBuffer);
			if ( SUCCEEDED(hr) )
			{
				*ppResourceMap = CXString::CloneBuffer(pBuffer, bufferLength);
				if ( *ppResourceMap )
				{

					delete[] pBuffer;
					pBuffer = NULL;
					if ( ppResourcePath || ppFilePath )
					{
						hr = pUri->GetPath(&bufferLength, NULL);
						if ( SUCCEEDED(hr) )
						{
							++bufferLength;
							pBuffer = (XCHAR * )new char[(2 * bufferLength | -((unsigned __int64)bufferLength >> 31 != 0))];

							if ( pBuffer )
							{
								hr = pUri->GetPath(&bufferLength, pBuffer);
								if ( SUCCEEDED(hr) )
								{
									pResourcePath = CXString::CloneBuffer(pBuffer, bufferLength);
									if ( pResourcePath )
									{
										delete[] pBuffer;
										pBuffer = NULL;
										if ( !ppResourcePath || (*ppResourcePath = pResourcePath->Clone(TRUE, FALSE)) != NULL )
										{
											if ( ppFilePath )
											{
												for (int i = 0; ; ++i )
												{
													if ( i >= 2 )
													{
														*ppFilePath = NULL;
														goto ClearAndExit;
													}

													pPathPrefix = CXString::WrapBuffer((UINT32)xstrlen((const XCHAR * )pPathPrefixes[i]), (XCHAR *)pPathPrefixes[i]);
													if ( pResourcePath->StartsWith(pPathPrefix, xstrCompareCaseInsensitive) )
														break;
		
													if ( pPathPrefix )
														delete pPathPrefix;
													pPathPrefix = NULL;
												}

												*ppFilePath = pResourcePath->SubString(pPathPrefix->GetCount(), pResourcePath->GetCount());
												if ( !*ppFilePath )
												{
													hr = 0x8007000Eu;
													if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
													{
														GetPALDebuggingServices()->XcpTrace(
															5u,
															L"windows\\dxaml\\xcp\\pal\\common\\msresourcehelpers.cpp",
															122,
															hr,
															L"*ppFilePath",
															0);
													}
												}
											}
										}
										else
										{
											hr = 0x8007000Eu;

											if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
											{
												GetPALDebuggingServices()->XcpTrace(
													5u,
													L"windows\\dxaml\\xcp\\pal\\common\\msresourcehelpers.cpp",
													109,
													hr,
													L"*ppResourcePath = pResourcePath->Clone()",
													0);
											}
										}
									}
									else
									{
										hr = 0x8007000Eu;

										if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
										{
											GetPALDebuggingServices()->XcpTrace(
												5u,
												L"windows\\dxaml\\xcp\\pal\\common\\msresourcehelpers.cpp",
												103,
												hr,
												L"pResourcePath",
												0);
										}
									}
								}
								else
								{
									if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
									{
										GetPALDebuggingServices()->XcpTrace(
											4u,
											L"windows\\dxaml\\xcp\\pal\\common\\msresourcehelpers.cpp",
											101,
											hr,
											L"pUri->GetPath(&bufferLength, pBuffer)",
											0);
									}
								}
							}
							else
							{
								hr = 0x8007000Eu;
								if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
								{
									GetPALDebuggingServices()->XcpTrace(
										5u,
										L"windows\\dxaml\\xcp\\pal\\common\\msresourcehelpers.cpp",
										100,
										hr,
										L"pBuffer",
										0);
								}
							}
						}
						else
						{
							if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
							{

								GetPALDebuggingServices()->XcpTrace(
									4u,
									L"windows\\dxaml\\xcp\\pal\\common\\msresourcehelpers.cpp",
									98,
									hr,
									L"pUri->GetPath(&bufferLength, NULL)",
									0);
							}
						}
					}
				}
				else
				{
					hr = 0x8007000Eu;
					if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
					{
						GetPALDebuggingServices()->XcpTrace(
							5u,
							L"windows\\dxaml\\xcp\\pal\\common\\msresourcehelpers.cpp",
							91,
							hr,
							L"*ppResourceMap",
							0);
					}
				}
			}
			else
			{
				if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
				{
					GetPALDebuggingServices()->XcpTrace(
						4u,
						L"windows\\dxaml\\xcp\\pal\\common\\msresourcehelpers.cpp",
						89,
						hr,
						L"pUri->GetHost(&bufferLength, pBuffer)",
						0);
				}
			}
		}
		else
		{
			hr = 0x8007000Eu;
			if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
			{
				GetPALDebuggingServices()->XcpTrace(5u, L"windows\\dxaml\\xcp\\pal\\common\\msresourcehelpers.cpp", 88, hr, L"pBuffer", 0);
			}
		}
	}
	else
	{
		if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
		{
			GetPALDebuggingServices()->XcpTrace(
				4u,
				L"windows\\dxaml\\xcp\\pal\\common\\msresourcehelpers.cpp",
				86,
				hr,
				L"pUri->GetHost(&bufferLength, NULL)",
				0);
		}
	}
ClearAndExit:
	delete[] pBuffer;

	if ( pPathPrefix )
		delete pPathPrefix;

	if ( pResourcePath )
		delete pResourcePath;
	return hr;
}

unsigned int MsResourceHelpers::IsMsResourceUri( CXString *uri )
{
	CXString * s = new CXString(LEN_STR_PAIR(L"ms-resource"));

	unsigned int r  = uri->StartsWith(s, xstrCompareCaseInsensitive);

	delete s;

	return r;
}

HRESULT MsResourceHelpers::IsMsResourceUri( IPALUri *pUri, unsigned int *pResult )
{

	XCHAR * pScheme = NULL;
	unsigned int length =0;
	HRESULT hr = S_OK;


	*pResult = NULL;
	hr = pUri->GetScheme(&length, NULL);
	if ( SUCCEEDED(hr) )
	{
		if ( length == xstrlen(L"ms-resource") )
		{
			++length;
			pScheme = (XCHAR * )new char[(2 * length | -((unsigned __int64)length >> 31 != 0))];
			if ( pScheme )
			{
				hr = pUri->GetScheme(&length,pScheme);
				if ( SUCCEEDED(hr) )
				{
					*pResult = xstrncmpi((const XCHAR * )pScheme, L"ms-resource", length) == 0;
				}
				else
				{
					if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
					{
						GetPALDebuggingServices()->XcpTrace(
							4u,
							L"windows\\dxaml\\xcp\\pal\\common\\msresourcehelpers.cpp",
							39,
							hr,
							L"pUri->GetScheme(&length, pScheme)",
							0);
					}
				}
			}
			else
			{
				hr = 0x8007000Eu;
				if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
				{
					GetPALDebuggingServices()->XcpTrace(5u, L"windows\\dxaml\\xcp\\pal\\common\\msresourcehelpers.cpp", 38, hr, L"pScheme", 0);
				}
			}
		}
	}
	else
	{
		if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
		{
			GetPALDebuggingServices()->XcpTrace(
				4u,
				L"windows\\dxaml\\xcp\\pal\\common\\msresourcehelpers.cpp",
				31,
				hr,
				L"pUri->GetScheme(&length, NULL)",
				0);
		}
	}
	delete[] pScheme;
	return hr;
}
